package com.hockeo.tinygds;

import java.lang.reflect.Field;

import com.google.appengine.api.datastore.GeoPt;
import com.hockeo.tinygds.error.TNotLoadableException;
import com.hockeo.tinygds.wrappers.TGeoPt;

/**
 *
 * @version $Id$
 * @author jjanke
 */
public class TFieldGeoPt extends TField<TGeoPt>
{
  protected TFieldGeoPt( TinyGDSFactory factory, Field field )
  {
    super( factory, field );
  }

  @Override
  public Object getValueForDatastore( Object objInstance )
  {
    TGeoPt geopt = getValue( objInstance );

    if ( geopt == null )
      return null;

    return new GeoPt( geopt.getLatitude(), geopt.getLongitude() );
  }

  @Override
  protected TGeoPt transformDatastoreValue( Object objDatastoreValue )
  {
    if ( objDatastoreValue instanceof GeoPt )
    {
      GeoPt geopt = (GeoPt) objDatastoreValue;

      return new TGeoPt( geopt.getLatitude(), geopt.getLongitude() );
    }

    throw TNotLoadableException.cannotConvertValue( objDatastoreValue, getRealField() );
  }
}
